﻿function SaveStroage_SuperMark(Data, callback)
{
	chrome.storage.sync.set({'WZY_NEW_TAB_VALUE': Data}, function(){
			callback(false);
		});	
}

function LoadStroage_SuperMark(callback, contetx)
{
	// 获取内容
	chrome.storage.sync.get({
		WZY_NEW_TAB_VALUE: "null",
	}, function(items) {
		var value = items.WZY_NEW_TAB_VALUE;

		if(value == "null"){
			value={};
			value.Menu = 1;
			value.list=new Array();
		}
		else if(typeof(value.Menu) == "undefined"){
			// 兼容旧版
			var newvalue={};
			newvalue.Menu = 1;
			newvalue.list=value;
			value = newvalue;
		}
		
		callback(value, contetx);
	});	
}

function SaveStroage_QuickStart(Data, callback, context)
{
	chrome.storage.sync.set({'WZY_HELLOPAGE_QUICKSTART': Data}, function(){
			callback(context);
		});	
}

function LoadStroage_QuickStart(callback, contetx)
{
	// 获取内容
	chrome.storage.sync.get({
		WZY_HELLOPAGE_QUICKSTART: "null",
	}, function(items) {
		var value = items.WZY_HELLOPAGE_QUICKSTART;

		if(value == "null"){
			value=new Array();
		}
		
		callback(value, contetx);
	});	
}

function LoadStroage_BookMark_Packet(obj)
{
	var rv = new Array;
	for(var i in obj){
		var cell = obj[i];		
		var haschild = (typeof($(cell).attr("children"))!="undefined");
		
		if(haschild){
			rv = rv.concat(LoadStroage_BookMark_Packet(cell.children));
		}
		else{
			var tmp = {};
			tmp.URL = cell.url;
			tmp.Img = "chrome://favicon/"+cell.url;
			tmp.Key = cell.title;
			tmp.Title = cell.url;
			rv.unshift(tmp);			
		}
	}
	
	return rv;
}

function LoadStroage_BookMark_Callback(tree, callback, context)
{
	// 整理tree
	var bookmarkroot = tree[0];
	var bookmark = bookmarkroot.children[0].children;
	var other = bookmarkroot.children[1].children;
	//var
	var pkt_bookmark_List = LoadStroage_BookMark_Packet(bookmark);
	
	var pkt_other_List = LoadStroage_BookMark_Packet(other);
	
	var rv = pkt_bookmark_List.concat(pkt_other_List);
	callback(rv, context); 
}

function LoadStroage_BookMark(callback, context)
{
	// 获取内容
	chrome.bookmarks.getTree(function(tree){
		LoadStroage_BookMark_Callback(tree, callback, context);		
	});
}

function LoadStroage_Location(callback, context){	
	// 获取内容
	chrome.storage.local.get({
		WZY_HELLOPAGE_LOCATION: "null",
	}, function(items) {
		var value = items.WZY_HELLOPAGE_LOCATION;

		if(value == "null"){
			// 发送请求
			$.getJSON("https://hellopage.purpleshine.cn:8080/weather/GetLocation?jsoncallback=?",
			function(data){
			  callback(data.results[0].name, context);
			  
			  SaveStroage_Location(data.results[0].name, function(context){},"");
			});
		}
		else{					
			callback(value, context);
		}
	});	
	
}

function SaveStroage_Location(Data, callback, context){	
		chrome.storage.local.set({'WZY_HELLOPAGE_LOCATION': Data}, function(){
			callback(context);
		});	
}

function LoadStroage_BK(callback, context){	
	// 获取内容
	chrome.storage.sync.get({
		WZY_OPTIONS: "null",
	}, function(items) {
		var value = items.WZY_OPTIONS;

		if(value == "null"){
			value={};
		}
		
		callback(value, context);
	});	
}

function SaveStroage_BK(Data, callback, context){	
		chrome.storage.sync.set({'WZY_OPTIONS': Data}, function(){
			callback(context);
		});	
}

function LoadStroage_BK_Img(url, callback)
{
	// 获取内容
	chrome.storage.local.get({
		WZY_BK_IMG: "null",
	}, function(items) {
		var value = items.WZY_BK_IMG;

		if(value == "null"){
			callback("", false);
		}
		else{					
			if(url == value.url){
				callback(url, true);				
			}
			else{
				if(url != ""){
					// 从网上加载图片
					SaveStroage_BK_Img(url, callback);
				}
				else{
					// 返回失败
					callback("", false);
				}
			}
		}
	});		
}

function SaveStroage_BK_Img(url, callback)
{
	// 将图片变成 DataURI
	var img = new Image();
	img.onload = function() {
	  var canvas = document.createElement('canvas');
	  canvas.width = img.width;
	  canvas.height = img.height;
	  var ctx = canvas.getContext('2d');
	  ctx.drawImage(img);

	  // 这里就是你要的 DataURI
	  var DataURI = canvas.toDataURL();
	  // 保存下来
	  var savedata = {};
	  savedata.url = url;
	  savedata.data = DataURI;
	  
	  chrome.storage.local.set({'WZY_BK_IMG': savedata}, function(){
			callback(savedata, true);
		});
	}
	img.onError = function(){
		callback("", false);
	}
	img.src = url; // 这里写你的图片 url	
}

function LoadStroage_Color(callback, context)
{
	// 获取内容
	chrome.storage.sync.get({
		WZY_COLOR: "#FFFFFF",
	}, function(items) {
		var value = items.WZY_COLOR;
		
		callback(value, context);
	});	
}

function SaveStroage_Color(Data, callback, context)
{
	chrome.storage.sync.set({'WZY_COLOR': Data}, function(){
		callback(context);
	});		
}

function LoadStroage_BK_DATAURI(callback, context)
{
	// 获取内容
	chrome.storage.local.get({
		WZY_BK_DATAURI: "",
	}, function(items) {
		var value = items.WZY_BK_DATAURI;
		
		callback(value, context);
	});	
}

function SaveStroage_BK_DATAURI(Data, callback, context)
{
	chrome.storage.local.set({'WZY_BK_DATAURI': Data}, function(){
		callback(context);
	});		
}
